package com.zzyl.nursing.service.impl;

import java.util.Date;
import java.util.List;
        import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @author YJH
 * @date 2025-10-30
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        // 1.根据id查询护理计划
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);


        // 2.根据护理计划id查询关联的所有护理项目
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectNursingProjectPlanListByPlanId(id);
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;

    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param  nursingPlanDto  护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
//        return save(nursingPlan) ? 1 : 0;
        //保存护理计划数据
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlan.setCreateTime(new Date());
        nursingPlanMapper.insertNursingPlan(nursingPlan);


        //保存护理计划项目数据
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        int count = nursingProjectPlanMapper.batchInsert(projectPlans, nursingPlan.getId());
        return count > 0 ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1.修改护理计划数据
        NursingPlan nursingPlan1 = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan1);
        int count = nursingPlanMapper.updateNursingPlan(nursingPlan1);


        //2.修改护理计划项目数据
        //2.1先删除中间表数据
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(nursingPlan1.getId());
        //2.2批量新增
        nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans(), nursingPlan1.getId());





        return count;

//        return updateById(nursingPlan) ? 1 : 0;

    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
                return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Long id)
    {
        int count = nursingPlanMapper.deleteNursingPlanById(id);
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(id);
                return count;
    }
}